package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.service.IYyClassService;
import com.ruoyi.system.service.IYyCourseService;
import com.ruoyi.system.service.IYyRoomService;
import com.ruoyi.system.service.impl.YyClassServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.YyStudent;
import com.ruoyi.system.service.IYyStudentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生Controller
 *
 * @author ruoyi
 * @date 2024-03-12
 */
@Controller
@RequestMapping("/system/student")
public class YyStudentController extends BaseController
{
    private String prefix = "system/student";

    @Autowired
    private IYyStudentService yyStudentService;

    @Autowired
    private IYyClassService yyClassService;

    @Autowired
    private IYyRoomService yyRoomService;

    @Autowired
    private IYyCourseService yyCourseService;

    @Autowired
    private SysPasswordService passwordService;

    @RequiresPermissions("system:student:view")
    @GetMapping()
    public String student()
    {
        return prefix + "/student";
    }

    /**
     * 查询学生列表
     */
    @RequiresPermissions("system:student:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YyStudent yyStudent)
    {
        yyStudent.setUserId(getUserId());
        startPage();
        List<YyStudent> list = yyStudentService.selectYyStudentList(yyStudent);
        return getDataTable(list);
    }

    /**
     * 导出学生列表
     */
    @RequiresPermissions("system:student:export")
    @Log(title = "学生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YyStudent yyStudent)
    {
        List<YyStudent> list = yyStudentService.selectYyStudentList(yyStudent);
        ExcelUtil<YyStudent> util = new ExcelUtil<YyStudent>(YyStudent.class);
        return util.exportExcel(list, "学生数据");
    }

    /**
     * 新增学生
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("class", yyClassService.listAll());
        mmap.put("room", yyRoomService.listAll());
        mmap.put("course", yyCourseService.listAll());
        return prefix + "/add";
    }

    /**
     * 新增保存学生
     */
    @RequiresPermissions("system:student:add")
    @Log(title = "学生", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YyStudent yyStudent)
    {
        try {
            //新增系统用户
            SysUser sysUser = new SysUser();
            sysUser.setUserName(yyStudent.getStuName());
            sysUser.setLoginName(yyStudent.getpAccount());
            sysUser.setSalt(ShiroUtils.randomSalt());
            sysUser.setPassword(passwordService.encryptPassword(sysUser.getLoginName(), yyStudent.getpPassword(), sysUser.getSalt()));


            return toAjax(yyStudentService.insertYyStudent(yyStudent, sysUser));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改学生
     */
    @RequiresPermissions("system:student:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        YyStudent yyStudent = yyStudentService.selectYyStudentById(id);
        mmap.put("yyStudent", yyStudent);
        mmap.put("class", yyClassService.selectYyClassByIdList(yyStudent.getClassId()));
        mmap.put("room", yyRoomService.selectYyRoomByIdList(yyStudent.getRoomId()));
        mmap.put("course", yyCourseService.selectYyCourseByIdList(yyStudent.getCourseId()));
        return prefix + "/edit";
    }

    /**
     * 修改保存学生
     */
    @RequiresPermissions("system:student:edit")
    @Log(title = "学生", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YyStudent yyStudent) throws Exception {
        return toAjax(yyStudentService.updateYyStudent(yyStudent));
    }

    /**
     * 删除学生
     */
    @RequiresPermissions("system:student:remove")
    @Log(title = "学生", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(yyStudentService.deleteYyStudentByIds(ids));
    }
}
